﻿using System;
using System.Globalization;
using System.Text.RegularExpressions;
using System.Text;

using System.Net;
using System.IO;


namespace SMS.ProcessUtils
{
	public class Utility
	{

        public Utility() { }

        #region xu ly nan tin Auto
        public static string GetMsgStockFromURL(string url)
        {
            string strval = "";
            WebRequest WQob = WebRequest.Create(url);
            WebResponse WPob = WQob.GetResponse();
            StreamReader SRob = new StreamReader(WPob.GetResponseStream());
            strval = SRob.ReadToEnd();
            WQob.Abort();
            return strval;
        }
        #endregion

        public static string GetMinuteString(string val)
        {
            try
            {
                CultureInfo vnCulture = new CultureInfo("vi-VN");
                TimeSpan span1;
                TimeSpan span2;
                int min;
                string[] vals = val.Split('^');

                if (vals[3] == "1") return "FT";

                if (vals[2] == "1")
                {
                    span1 = DateTime.Now.Subtract(DateTime.Parse(vals[0], vnCulture));

                    if ((span1.Hours * 60 + span1.Minutes) >= 0 && (span1.Hours * 60 + span1.Minutes) <= 45)
                    {
                        min = (span1.Hours * 60 + span1.Minutes);
                        return String.Format("{0}'", min.ToString());
                    }
                    if ((span1.Hours * 60 + span1.Minutes) < 0) return "";

                    span2 = DateTime.Now.Subtract(DateTime.Parse(vals[1], vnCulture));

                    if ((span2.Hours * 60 + span2.Minutes) >= 0 && (span2.Hours * 60 + span2.Minutes) <= 45)
                    {
                        min = 45 + (span2.Hours * 60 + span2.Minutes);
                        return String.Format("{0}'", min.ToString());
                    }

                    if ((span2.Hours * 60 + span2.Minutes) < 0) return "45'";
                }

                return "90'";
            }
            catch { return ""; }     
        }

        public static string RemoveSpecialChar(string unicodeString)
        {
            try
            {
                // Remove VietNamese character
                unicodeString = unicodeString.ToLower();
                unicodeString = Regex.Replace(unicodeString, "[áàảãạâấầẩẫậăắằẳẵặ]", "a");
                unicodeString = Regex.Replace(unicodeString, "[éèẻẽẹêếềểễệ]", "e");
                unicodeString = Regex.Replace(unicodeString, "[iíìỉĩị]", "i");
                unicodeString = Regex.Replace(unicodeString, "[óòỏõọơớờởỡợôốồổỗộ]", "o");
                unicodeString = Regex.Replace(unicodeString, "[úùủũụưứừửữự]", "u");
                unicodeString = Regex.Replace(unicodeString, "[yýỳỷỹỵ]", "y");
                unicodeString = Regex.Replace(unicodeString, "[đ]", "d");

                // Remove space
                unicodeString = unicodeString.Replace(" ", "");

                // Remove special character
                //unicodeString = Regex.Replace(unicodeString, "[`-=+:;'?/><,.]", "");
                unicodeString = Regex.Replace(unicodeString, "[`!-+=?/>.<,{}[]|]\\]", "");

                return unicodeString;
            }
            catch (Exception)
            {
                return "";
            }
        }

        public static string UnsignText(string text)
        {
            return UnsignText(text, ' ');
        }

        public static string UnsignText(string text, char spaceChar)
        {
            char[] charr = text.ToCharArray();
            StringBuilder sb = new StringBuilder();

            for (int i = 0; i < charr.Length; i++)
            {
                char c = char.ToLower(charr[i]);
                switch (c)
                {
                    case 'á':
                    case 'à':
                    case 'ả':
                    case 'ã':
                    case 'ạ':
                    case 'â':
                    case 'ấ':
                    case 'ầ':
                    case 'ẩ':
                    case 'ẫ':
                    case 'ậ':
                    case 'ă':
                    case 'ắ':
                    case 'ằ':
                    case 'ẳ':
                    case 'ẵ':
                    case 'ặ':
                        c = 'a'; break;

                    case 'đ':
                        c = 'd'; break;

                    case 'é':
                    case 'è':
                    case 'ẻ':
                    case 'ẽ':
                    case 'ẹ':
                    case 'ê':
                    case 'ế':
                    case 'ề':
                    case 'ể':
                    case 'ễ':
                    case 'ệ':
                        c = 'e'; break;

                    case 'í':
                    case 'ì':
                    case 'ỉ':
                    case 'ĩ':
                    case 'ị':
                        c = 'i'; break;

                    case 'ó':
                    case 'ò':
                    case 'ỏ':
                    case 'õ':
                    case 'ọ':
                    case 'ô':
                    case 'ố':
                    case 'ồ':
                    case 'ổ':
                    case 'ỗ':
                    case 'ộ':
                    case 'ơ':
                    case 'ớ':
                    case 'ờ':
                    case 'ở':
                    case 'ỡ':
                    case 'ợ':
                        c = 'o'; break;

                    case 'ú':
                    case 'ù':
                    case 'ủ':
                    case 'ũ':
                    case 'ụ':
                    case 'ư':
                    case 'ứ':
                    case 'ừ':
                    case 'ử':
                    case 'ữ':
                    case 'ự':
                        c = 'u'; break;

                    case 'ý':
                    case 'ỳ':
                    case 'ỷ':
                    case 'ỹ':
                    case 'ỵ':
                        c = 'y'; break;

                    case ' ':
                        c = spaceChar; break;
                }

                if (char.IsUpper(charr[i]) && c != char.MinValue) c = char.ToUpper(c);
                if (c != char.MinValue) sb.Append(c);
            }

            return sb.ToString();
        }

        public static string UnsignText(string text, bool toQuery)
        {
            char[] charr = text.ToCharArray();
            StringBuilder sb = new StringBuilder();

            for (int i = 0; i < charr.Length; i++)
            {
                char c = char.ToLower(charr[i]);
                switch (c)
                {
                    case 'á':
                    case 'à':
                    case 'ả':
                    case 'ã':
                    case 'ạ':
                    case 'â':
                    case 'ấ':
                    case 'ầ':
                    case 'ẩ':
                    case 'ẫ':
                    case 'ậ':
                    case 'ă':
                    case 'ắ':
                    case 'ằ':
                    case 'ẳ':
                    case 'ẵ':
                    case 'ặ':
                        c = 'a'; break;

                    case 'đ':
                        c = 'd'; break;

                    case 'é':
                    case 'è':
                    case 'ẻ':
                    case 'ẽ':
                    case 'ẹ':
                    case 'ê':
                    case 'ế':
                    case 'ề':
                    case 'ể':
                    case 'ễ':
                    case 'ệ':
                        c = 'e'; break;

                    case 'í':
                    case 'ì':
                    case 'ỉ':
                    case 'ĩ':
                    case 'ị':
                        c = 'i'; break;

                    case 'ó':
                    case 'ò':
                    case 'ỏ':
                    case 'õ':
                    case 'ọ':
                    case 'ô':
                    case 'ố':
                    case 'ồ':
                    case 'ổ':
                    case 'ỗ':
                    case 'ộ':
                    case 'ơ':
                    case 'ớ':
                    case 'ờ':
                    case 'ở':
                    case 'ỡ':
                    case 'ợ':
                        c = 'o'; break;

                    case 'ú':
                    case 'ù':
                    case 'ủ':
                    case 'ũ':
                    case 'ụ':
                    case 'ư':
                    case 'ứ':
                    case 'ừ':
                    case 'ử':
                    case 'ữ':
                    case 'ự':
                        c = 'u'; break;

                    case 'ý':
                    case 'ỳ':
                    case 'ỷ':
                    case 'ỹ':
                    case 'ỵ':
                        c = 'y'; break;

                    case ' ':
                        if (sb.Length > 1 && sb[sb.Length - 1] != '_') c = '_';
                        else continue;
                        break;

                    default:
                        if (!char.IsLetterOrDigit(c) && toQuery) continue;
                        c = charr[i];
                        break;
                }

                if (char.IsUpper(charr[i]) && c != char.MinValue) c = char.ToUpper(c);
                if (c != char.MinValue) sb.Append(c);
            }
            return sb.ToString();
        }

        public static bool IsNumeric(string s)
        {
            if (string.IsNullOrEmpty(s)) return false;

            foreach (char c in s)
            {
                if (!char.IsDigit(c)) return false;
            }
            return true;
        }

		// Replace all duplicate characters in string with a single instance
		public static string TrimAll(string TextIn, string TrimChar, string CtrlChar)
		{
			string strTrim = "";
			try
			{
                // Trim begining and ending
				strTrim = strTrim.Trim();
				strTrim = TextIn.Replace(TrimChar, CtrlChar);
				while(strTrim.IndexOf(CtrlChar + CtrlChar) > 0)
				{
					strTrim = strTrim.Replace(CtrlChar + CtrlChar, CtrlChar);
				}				
			}
			catch(Exception ex)
			{
				strTrim = TextIn;
				throw ex;
			}
			return strTrim;				
		}

		bool ValidatePhone(string FaxNum)
		{
			bool RtnVal = true;

			if(FaxNum.Length != 11)
				RtnVal = false;
			else
			{
				int Pos;
				int NumChars;

				NumChars = FaxNum.Length;
				FaxNum.ToUpper();

				for(Pos = 0; Pos < NumChars; Pos ++)
				{
					
				}
			}
			return RtnVal;
		}
       
            
        public static string CatChuoi(string s, int length)
        {
            /// <summary>
            /// Trả về phần đầu của chuỗi và đảm bảo đủ từ
            /// </summary>
            /// <param name="s"></param>
            /// <param name="length">Số ký tự tối đa được cắt</param>
            /// <returns>String</returns>
            if (String.IsNullOrEmpty(s))
                throw new ArgumentNullException(s);
            var words = s.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
            if (words[0].Length > length)
                throw new ArgumentException("Từ đầu tiên dài hơn chuỗi cần cắt");
            var sb = new StringBuilder();
            foreach (var word in words)
            {
                if ((sb + word).Length > length)
                    return string.Format("{0}", sb.ToString().TrimEnd(';'));
                sb.Append(word + ";");
            }
            return string.Format("{0}", sb.ToString().TrimEnd(';'));
        }

        
	}
}
